package com.xydtech.ecard.busi.controller;

import com.xydtech.common.annotation.RsaRequest;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportConfig;
import com.xydtech.ecard.busi.domain.EcardApiBusiReportOther;
import com.xydtech.ecard.busi.service.IEcardApiBusiReportConfigService;
import com.xydtech.ecard.busi.service.IEcardApiBusiReportOtherService;
import com.xydtech.ecard.ecard.controller.EcardApiBaseController;
import com.xydtech.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 电子名片业务填报其他业务类信息Controller
 *
 * @author yuzhiguo
 * @date 2024-09-26 下午3:40
 */
@Controller
@Slf4j
@Api(tags = "电子名片-业务填报其他业务类接口")
@RequestMapping("/ecardSaasApi/busi/reportOther")
public class EcardApiBusiReportOtherController extends EcardApiBaseController {

    @Autowired
    private IEcardApiBusiReportOtherService ecardBusiReportOtherService;
    @Autowired
    private IEcardApiBusiReportConfigService ecardBusiReportConfigService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 电子名片-保存业务填报其他业务类数据接口
     *
     * @author yuzhiguo
     * @date 2024/9/26 下午1:45
     */
    @PostMapping(value = "/addReportOtherInfo")
    @ApiOperation(value = "电子名片-保存业务填报其他业务类数据接口", notes = "电子名片-保存业务填报其他业务类数据接口")
    @ResponseBody
    @RsaRequest(result = false)
    public ResponseModel addReportOtherInfo(@RequestBody EcardApiBusiReportOther ecardBusiReportOther) {
        try {
            String salt = getSalt();//盐值
            SysDept dept = sysDeptService.selectSecondLevelDeptByDeptId(ecardBusiReportOther.getDeptId());
            //根据业务类型id查询业务类型信息
            String configId = ecardBusiReportOther.getConfigId();
            EcardApiBusiReportConfig reportConfig = ecardBusiReportConfigService.getReportConfigById(Long.valueOf(configId));
            if ("F".equals(reportConfig.getIsMore())){//是否可以多次保存，否的话根据身份证和业务类型id校验是否已存在
                EcardApiBusiReportOther reportOther = new EcardApiBusiReportOther();
                //数据加密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(reportOther.getOthCustIdcard())) {
                    reportOther.setOthCustIdcard(AESEncryptionUtils.encrypt(reportOther.getOthCustIdcard(), salt));//身份证号
                }
                reportOther.setDeptId(dept.getDeptId());
                reportOther.setConfigId(configId);
                List<EcardApiBusiReportOther> busiReportOtherList = ecardBusiReportOtherService.getMyReportOtherList(reportOther, salt);
                if (busiReportOtherList.size() > 0){
                    return ResponseModel.error("该客户已存在同类型业务！");
                }
            }
            //管户人id
            ecardBusiReportOther.setManagementPersonnelId(ecardBusiReportOther.getCreatorId());
            int id = ecardBusiReportOtherService.addReportOtherInfo(ecardBusiReportOther, salt);
            return id > 0 ? ResponseModel.ok(ecardBusiReportOther.getId()) : ResponseModel.error("添加失败！");
        } catch (Exception ex) {
            ex.printStackTrace();
            return ResponseModel.error("操作失败，请联系管理员！");
        }
    }
}
